package com.yonghui.springsecurityoauth2demo.exception;


import com.yonghui.springsecurityoauth2demo.pojo.ResultCode;
import com.yonghui.springsecurityoauth2demo.pojo.ResultVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidScopeException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.RedirectMismatchException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * @author 公众号：码猿技术专栏
 * 自定义异常翻译器，针对用户名、密码异常，授权类型不支持的异常进行处理
 */
@SuppressWarnings("ALL")
public class OAuthServerWebResponseExceptionTranslator implements WebResponseExceptionTranslator{
    /**
     * 业务处理方法，重写这个方法返回客户端信息
     */
    @Override
    public ResponseEntity<ResultVo> translate(Exception e){
        ResultVo resultMsg = doTranslateHandler(e);
        return new ResponseEntity<>(resultMsg, HttpStatus.UNAUTHORIZED);
    }

    /**
     * 根据异常定制返回信息
     * TODO 自己根据业务封装
     */
    private ResultVo doTranslateHandler(Exception e) {
        //初始值，系统错误，
        ResultCode resultCode = ResultCode.UNAUTHORIZED;
        //判断异常，不支持的认证方式
        if(e instanceof UnsupportedGrantTypeException){
            resultCode = ResultCode.UNSUPPORTED_GRANT_TYPE;
            //用户名或密码异常
        }else if(e instanceof InvalidGrantException){
            String message = e.getMessage();
            if(message.contains("Invalid authorization code")){
                resultCode = ResultCode.CODE_ERROR;
            }

            //resultCode = ResultCode.USERNAME_OR_PASSWORD_ERROR;
        }else if(e instanceof InvalidClientException){
            resultCode = ResultCode.CLIENT_ERROR;
        }else if(e instanceof InvalidScopeException){
            resultCode = ResultCode.SCOPE_ERROR;
        }else if(e instanceof RedirectMismatchException){
            resultCode = ResultCode.REDIRECT_URI_ERROR;
        } else if(e instanceof InvalidTokenException){
            resultCode = ResultCode.TOKEN_ERROR;
        }
        return new ResultVo(resultCode.getCode(),resultCode.getMsg(),null);
    }
}
